package com.iuv.shortmsg.service.impl;

import com.iuv.shortmsg.domain.DemoSmsUserTbl;
import com.iuv.shortmsg.repository.DemoSmsUserTblRepository;
import com.iuv.shortmsg.service.DemoSmsUserService;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * DemoSmsUserServiceImpl
 * 短信例子-用户业务实现类
 *
 * @author Will
 * @date 13:50
 */
@Service("demoSmsUserService")
public class DemoSmsUserServiceImpl implements DemoSmsUserService {
    @Autowired
    private DemoSmsUserTblRepository demoSmsUserTblRepository;

    /**
     * 创建用户帐户
     * @param mobile
     * @param password
     * @return
     */
    @Override
    public DemoSmsUserTbl createUser(String mobile, String password) {
        DemoSmsUserTbl demoSmsUserTbl = new DemoSmsUserTbl();
        Date now = Calendar.getInstance().getTime();
        demoSmsUserTbl.setCreateTime(now);
        demoSmsUserTbl.setLastUpdateTime(now);
        demoSmsUserTbl.setMobile(mobile);
        demoSmsUserTbl.setUserName(mobile);
        //非明文存储密码
        demoSmsUserTbl.setPassword(Md5Crypt.md5Crypt(password.getBytes()));
        return demoSmsUserTblRepository.save(demoSmsUserTbl);
    }

    /**
     * 检查电话号码是否存在
     * @param mobile
     * @return
     */
    @Override
    public boolean checkMobileExist(String mobile) {
        long count = 0;
        if (mobile != null) {
            count = demoSmsUserTblRepository.countByMobile(mobile);
        }
        return count > 0;
    }
}
